package yja.gzw.app;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.bytedance.sdk.open.aweme.CommonConstants;
import com.bytedance.sdk.open.aweme.authorize.model.Authorization;
import com.bytedance.sdk.open.aweme.common.handler.IApiEventHandler;
import com.bytedance.sdk.open.aweme.common.model.BaseReq;
import com.bytedance.sdk.open.aweme.common.model.BaseResp;
import com.bytedance.sdk.open.douyin.DouYinOpenApiFactory;
import com.bytedance.sdk.open.douyin.api.DouYinOpenApi;

import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import yja.gzw.app.model.UserData;
import yja.gzw.app.viewmodel.Mainviewmodel;

public class DouYinEntryActivity extends Activity implements IApiEventHandler {
   public static String  ss="";

    Mainviewmodel viewmodel;
    DouYinOpenApi douYinOpenApi;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        douYinOpenApi = DouYinOpenApiFactory.create(this);
        douYinOpenApi.handleIntent(getIntent(), this);
    }

    @Override
    public void onReq(BaseReq req) {

    }

    @Override
    public void onResp(BaseResp resp) {
        // 授权成功可以获得authCode
        if (resp.getType() == CommonConstants.ModeType.SEND_AUTH_RESPONSE) {
            Authorization.Response response = (Authorization.Response) resp;
            if (resp.isSuccess()) {


               ss = ((Authorization.Response) resp).authCode;
              UserData.shouquandouying=true;
                Toast.makeText(this, "授权成功，获得权限",
                        Toast.LENGTH_LONG).show();
                new OkHttpPostTask().execute();

            } else {
                Toast.makeText(this, "授权失败" + response.grantedPermissions,
                        Toast.LENGTH_LONG).show();
            }
            finish();
        }
    }

    @Override
    public void onErrorIntent(@Nullable Intent intent) {
        // 错误数据
        Toast.makeText(this, "intent出错啦", Toast.LENGTH_LONG).show();
        finish();
    }
}
 class OkHttpPostTask extends AsyncTask<Void, Void, String> {
    @Override
    protected String doInBackground(Void... voids) {
        OkHttpClient client = new OkHttpClient();
        // 设置请求体
        RequestBody formBody = new FormBody.Builder()
                .add("client_key", "aw8lj7m8q1ei9u93")
                .add("client_secret", "5e33f8f8c4741281537116dfe7e488bc")
                .add("code",DouYinEntryActivity.ss)
                .add("grant_type","authorization_code")
                .build();
        // 创建请求
        Request request = new Request.Builder()
                .url("https://open.douyin.com/oauth/access_token/")
                .addHeader("content-type", "application/x-www-form-urlencoded")
                .post(formBody)
                .build();
        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful()) {
                return response.body().string();
            } else {
                return "请求失败，状态码：" + response.code();
            }
        } catch (IOException e) {
            e.printStackTrace();
            return "请求出现异常：" + e.getMessage();
        }
    }

    @Override
    protected void onPostExecute(String result) {

        Log.e("抖音网络数据:",result);

    }
}